package com.example.demo.Interceptor;

import com.example.demo.utils.JwtUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：邓利
 * 时间：2024-06-05
 **/
//拦截器实现HandlerInterceptor 要写上配置类@Configuration
@Configuration
public class TokenInterceptor implements HandlerInterceptor {
    //在请求处理方法被调用之前被调用
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");//必须放在请示头Header
        if (!JwtUtil.checkToken(token)) {//验证失败
            return false;
        }
        return true;
    }
}
